package com.zsc.wxm.ysh.anjo.oss.service.impl;

import com.aliyun.oss.OSSClient;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zsc.wxm.ysh.anjo.oss.model.OssConnectInfo;
import com.zsc.wxm.ysh.anjo.oss.service.FileService;
import com.zsc.wxm.ysh.anjo.service.base.constant.OtherConstant;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 文件服务
 * @author playmaker
 * @since 2021-01-12
 */
@Service
public class FileServiceImpl implements FileService {

    @Resource
    private OssConnectInfo ossConnectInfo;

    @Override
    public String upload(MultipartFile file) throws IOException {
        //创建OSSClient
        OSSClient ossClient = new OSSClient(ossConnectInfo.getEndpoint(), ossConnectInfo.getAccesskeyId(), ossConnectInfo.getAccesskeySecret());
        //文件格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");

        String fileName = ossConnectInfo.getFolder() + "/" + simpleDateFormat.format(new Date()) + ".jpg";

        //上传
        ossClient.putObject(ossConnectInfo.getBucketName(), fileName, new ByteArrayInputStream(file.getBytes()));

        ossClient.shutdown();

        StringBuffer url = new StringBuffer();
        url.append("https://").append(ossConnectInfo.getBucketName()).append(".").append(ossConnectInfo.getEndpoint())
                .append("/").append(fileName);

        return url.toString();
    }

    @Override
    public void removeImage(String url) {
        //创建OSSClient
        OSSClient ossClient = new OSSClient(ossConnectInfo.getEndpoint(), ossConnectInfo.getAccesskeyId(), ossConnectInfo.getAccesskeySecret());
        //文件删除
        String host = "https://playmaker.oss-cn-shenzhen.aliyuncs.com/anjo/";
        String objectName = url.substring(host.length() + 1);
        ossClient.deleteObject(ossConnectInfo.getBucketName(), objectName);
        // 关闭OSSClient。
        ossClient.shutdown();
    }

    @Override
    public void uploadHouseImage() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        //创建OSSClient
        OSSClient ossClient = new OSSClient(ossConnectInfo.getEndpoint(), ossConnectInfo.getAccesskeyId(), ossConnectInfo.getAccesskeySecret());
        //读取data.json
        List<String> lines = FileUtils.readLines(new File(OtherConstant.JSON_DATA_PATH + "data.json"),"UTF-8");
        for (String line : lines) {
            JsonNode jsonNode = mapper.readTree(line);
            String image = jsonNode.get("image").asText();
            String imageName = ossConnectInfo.getFolder() + "/" + image;
            ossClient.putObject(ossConnectInfo.getBucketName(),imageName,new File(OtherConstant.IMAGE_SAVE_PATH + image));
        }

    }

}
